package com.kofigyan.stateprogressbar.utils;

import ohos.agp.text.Font;
import ohos.agp.utils.TextTool;
import ohos.app.Context;
import ohos.global.resource.RawFileEntry;
import ohos.global.resource.Resource;
import ohos.global.resource.ResourceManager;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Kofi Gyan on 5/22/2016.
 */

public class FontManager {

    private static final Map<String, Font> mFontCache = new HashMap<String, Font>();

    private static final String FONTAWESOME = "fonts/fontawesome-webfont.ttf";

    public static Font getTypeface(Context context) throws IOException {

        Font typeface = mFontCache.get(FONTAWESOME);

        if (typeface == null) {
            typeface = setTypeface(context, FONTAWESOME);
//            typeface = Font.createFromAsset(context.getAssets(), FONTAWESOME);
            mFontCache.put(FONTAWESOME, typeface);
        }

        return typeface;
    }


    public static Font getTypeface(Context context, final String filePath) {
        synchronized (mFontCache) {
            try {
                if (!mFontCache.containsKey(filePath)) {
                    final Font typeface = setTypeface(context, filePath);
//                    final Font typeface = Font.createFromAsset(context.getAssets(), filePath);
                    mFontCache.put(filePath, typeface);
                    return typeface;
                }
            } catch (Exception e) {
                LogUtil.warn("StateProgressBar", "Cannot create asset from "
                        + filePath + ". Ensure you have passed in the correct path and file name." + e);
                mFontCache.put(filePath, null);
                return null;
            }
            return mFontCache.get(filePath);
        }
    }
    public static Font setTypeface(Context context, String typeface) throws IOException {
        if (TextTool.isNullOrEmpty(typeface)) {
            return null;
        }

        File file = new File(context.getCodeCacheDir(), typeface);
        OutputStream outputStream = null;
        ResourceManager resManager = context.getResourceManager();
        RawFileEntry rawFileEntry = resManager.getRawFileEntry("resources/rawfile/fonts/" + typeface);

        Resource resource = null;
        try {
            resource = rawFileEntry.openRawFile();

            outputStream = new FileOutputStream(file);
            int index;
            byte[] bytes = new byte[1024];
            if (resource != null) {
                while ((index = resource.read(bytes)) != -1) {
                    outputStream.write(bytes, 0, index);
                    outputStream.flush();
                }
                Font.Builder builder = new Font.Builder(file);
                Font font = builder.build();
                return font;
            }
        } catch (FileNotFoundException | NullPointerException ignored) {
        } finally {
            resource.close();
            outputStream.close();
        }
        return null;
    }

}